package com.zm.spring.jdbc;

import com.zm.spring.jdbc.Bean.Account;
import com.zm.spring.jdbc.dao.AccountDao;
import com.zm.spring.jdbc.dao.OpRecordDao;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BasicServlet{
    protected void log(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        ApplicationContext cxt = (ApplicationContext) servletContext.getAttribute("cxt");
        AccountDao dao = cxt.getBean(AccountDao.class);

        int id = Integer.parseInt(req.getParameter("userId"));
        String password = req.getParameter("password");

        Account account = new Account();
        account.setId(id);
        account.setPassword(password);
        Account account1 = dao.selectByAccount(account);
        System.out.println("account1 = " + account1);
        if (account1!=null){
            req.getSession().setAttribute("userInfo",account1);
            this.send(resp,1);
            return;
        }
        this.send(resp,0);

    }

    protected void logOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("userInfo");
        this.send(resp,1);

    }
}
